<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/utils/api'
import Toast from '@/components/Toast.vue'
import { showToast } from '@/utils/toast'

const router = useRouter()
const phone = ref('') // 手机号
const verifyCode = ref('') // 验证码
const newPassword = ref('') // 新密码
const confirmPassword = ref('') // 确认密码
const username = ref('') // 用户名
const showPasswordForm = ref(false) // 控制密码表单显示

// 验证码倒计时相关
const countdown = ref(0) // 倒计时秒数
const timer = ref(null) // 定时器引用

// 修改错误提示的方法
const showError = (message) => {
  showToast(message, 'error')
}

// 修改成功提示的方法
const showSuccess = (message) => {
  showToast(message, 'success')
}

// 获取验证码
const handleGetVerifyCode = async () => {
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    showError('请输入有效的11位手机号码')
    return
  }

  // 生成验证码
  try {
    const response = await api.post('/api/verify_code', {
      phone: phone.value,
    })
    if (response.data.code === 0) {
      showToast(response.data.message, 'success')
    }
    else{
      showToast(response.data.message, 'error')
    }
  } catch (error) {
    showToast(error.message, 'error')
  }

  // 开始倒计时
  countdown.value = 60
  timer.value = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer.value)
    }
  }, 1000)
}

// 验证手机验证码
const handleVerifyCode = async () => {
  if (!/^\d{4}$/.test(verifyCode.value)) {
    showError('验证码必须是4位数字')
    return
  }

  try {
    const response = await api.post('/api/verify', {
      phone: phone.value.toString(),
      verifyCode: verifyCode.value.toString(),
    })

    if (response.data.code === 0) {
      showPasswordForm.value = true
      username.value = response.data.username
    } else {
      showError(response.data.message)
    }
  } catch (error) {
    showError(error.message)
  }
}

// 重置密码
const handleResetPassword = async () => {
  if (!newPassword.value || !confirmPassword.value) {
    showError('请输入密码和确认密码')
    return
  }

  if (newPassword.value.length < 6 || newPassword.value.length > 16) {
    showError('密码长度至少为6位，最多16位')
    return
  }

  if (newPassword.value !== confirmPassword.value) {
    showError('两次输入的密码不一致')
    return
  }

  try {
    const response = await api.post('/api/reset-password', {
      phone: phone.value,
      password: newPassword.value
    })
    if (response.data.code === 0) {
      showSuccess('密码重置成功！正在跳转到登录页...')
      setTimeout(() => {
        router.push('/')
      }, 2000)
    } else {
      showError(response.data.message)
    }
  } catch (error) {
    showError(error.message)
  }
}
</script>

<template>
  <div class="login-container">
    <Toast />
    <div class="login-box">
      <h2>重置密码</h2>
      <div class="login-form">
        <template v-if="!showPasswordForm">
          <div class="form-item">
            <input 
              type="text"
              v-model="phone"
              placeholder="请输入手机号"
            >
          </div>
          <div class="form-item verify-code">
            <input 
              type="text"
              v-model="verifyCode"
              placeholder="请输入验证码"
            >
            <button 
              class="code-btn"
              @click="handleGetVerifyCode"
              :disabled="countdown > 0"
            >
              {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
            </button>
          </div>
          <button @click="handleVerifyCode">验证</button>
        </template>

        <template v-else>
          <div class="form-item">
            <p class="username">用户名：{{ username }}</p>
          </div>
          <div class="form-item">
            <input 
              type="password"
              v-model="newPassword"
              placeholder="请输入新密码"
            >
          </div>
          <div class="form-item">
            <input 
              type="password"
              v-model="confirmPassword"
              placeholder="请再次输入新密码"
              @keyup.enter="handleResetPassword"
            >
          </div>
          <button @click="handleResetPassword">重置密码</button>
        </template>

        <div class="register-link">
          <router-link to="/">返回登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  color: #333;
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-item {
  position: relative;
}

input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 16px;
  transition: all 0.3s;
}

input:focus {
  border-color: #667eea;
  outline: none;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

button {
  width: 100%;
  padding: 12px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

button:hover {
  background: #5a6fd6;
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

.error-message {
  color: #ff4d4f;
  font-size: 16px;
  text-align: center;
}

.register-link {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  font-size: 16px;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
}

.register-link a:hover {
  color: #5a6fd6;
  opacity: 0.8;
}

.verify-code {
  display: flex;
  gap: 10px;
}

.verify-code input {
  flex: 1;
}

.code-btn {
  width: auto;
  white-space: nowrap;
  padding: 12px 15px;
}

.code-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
  transform: none;
}

.username {
  font-size: 16px;
  color: #333;
  margin: 0;
  padding: 12px 0;
}
</style> 